package flex.cc.vehicle_svr.controller;

import java.net.URLDecoder;
import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.ParserConfig;

import flex.cc.common.utils.DateUtil;
import flex.cc.core.support.BaseController;
import flex.cc.order.manage.service.OrderTrackService;
import flex.cc.vehicle_svr.model.SynStatusModel;
import flex.cc.vehicle_svr.vo.ResponseVo;

/**
 * 
 * @author WANGCONG
 * 2021年9月7日下午1:00:28
 * @desc 下游状态回写接口
 */
@RestController
@RequestMapping(value = "/vehicleSvr")
public class SynStatusController extends BaseController {

	private ParserConfig parserConfig = new ParserConfig ();
	
	@Resource
	private OrderTrackService orderTrackService;
	
	/**
	 * 
	 * @author WANGCONG
	 * 2021年9月7日下午1:03:15
	 * @desc 下游状态回写
	 */
	@ResponseBody
    @RequestMapping("/synStatus")
    public JSONObject synStatus(HttpServletRequest request,@RequestHeader Map<String,String> requestHeader) throws Exception {
    	//进入接口时间
    	long startTime = System.currentTimeMillis();
    	logger.info("vehicleSvr/synStatus 进入接口:time=" + DateUtil.getStrDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS"));
    	
    	logger.info("vehicleSvr/synStatus 请求头信息："+ requestHeader);
    	logger.info("Authorization=="+ requestHeader.get("authorization"));
    	//生成响应报文
        JSONObject jsonResult = new JSONObject();
        //读取报文
        try{
        	//设置编码
            request.setCharacterEncoding("UTF-8");
            //读取请求报文 
	        String requestJson = IOUtils.toString(request.getInputStream(), "UTF-8");
	        logger.info("vehicleSvr/synStatus 请求报文："+ requestJson);
	        if(requestJson != null && !"".equals(requestJson)) {
	        	requestJson = requestJson.toString().replaceAll("%(?![0-9a-fA-F]{2})", "%25").replaceAll("\\+", "%2B");
	        	requestJson = URLDecoder.decode(requestJson, "UTF-8");
		        logger.info("vehicleSvr/synStatus 转义解码后请求报文：" + requestJson);
	        	//调用业务处理 =============================
	        	SynStatusModel statusVo = JSON.parseObject (requestJson, SynStatusModel.class, parserConfig, JSON.DEFAULT_PARSER_FEATURE);
	        	if ( ObjectUtils.isEmpty ( statusVo ) || ObjectUtils.isEmpty ( statusVo.getOrderNo() ) ) { //报文为空或者救援单号为空
	        		jsonResult.put("resultCode", ResponseVo.ERROR_CODE);
	        		jsonResult.put("message", "报文格式不正确");
	        	}else{
	        		//业务。。。。
	        		Map<String, Object> resultMap = orderTrackService.synStatus_V2(statusVo);
	        		jsonResult.put("resultCode", resultMap.get("resultCode"));
	        		jsonResult.put("message", resultMap.get("message"));
	        	}
	        } else {
	        	logger.error("vehicleSvr/synStatus 接口处理异常:请求报文为空");
        		jsonResult.put("resultCode", ResponseVo.ERROR_CODE);
        		jsonResult.put("message", "报文为空或者救援单号为空");
	        }
        }catch(Exception e){
        	e.printStackTrace();
        	logger.error("vehicleSvr/synStatus 数据接收异常:" + e.getMessage());
    		jsonResult.put("resultCode", ResponseVo.ERROR_CODE);
    		jsonResult.put("message", "数据接收异常");
    		return jsonResult;
        }
        logger.info("vehicleSvr/synStatus responseJson:" + JSONObject.toJSONString(jsonResult));
        //离开接口时间
        long endTime = System.currentTimeMillis();
        logger.info("vehicleSvr/synStatus 离开接口:time=" + DateUtil.getStrDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS") + ";处理时长:" + (endTime - startTime));
//		jsonResult.put("resultCode", ResponseVo.SUCCESS_CODE); //若返回不成功，乙方会不断的进行回调
//		jsonResult.put("message", ResponseVo.SUCCESS_MSG);
		return jsonResult;
    }

}
